<?php

class m120429_052742_create_language extends CDbMigration {

	public function safeUp() {
		$language = array(
			'id'=>'pk',
			'pageId'=>'integer',
			'extension'=>'varchar(4)',
			'value'=>'varchar(32)',
			'name'=>'string',
			'email'=>'varchar(128)',
			'userId'=>'integer',
			'viewCount'=>'integer',
			'createdAt'=>'datetime',
			'updatedAt'=>'datetime',
		);
		$this->createTable('languages', $language);
		$this->createIndex('index_language_page', 'languages', 'pageId');
		$this->createIndex(
			'index_language_updated_at', 'languages', 'updatedAt'
		);

		$auth = Yii::app()->authManager;

		$auth->createOperation('languageUpdate', 'Update Language');
		$auth->createOperation('languageDelete', 'Delete Language');
		$auth->createOperation('languageManage', 'Manage Language');

		$role = $auth->createRole('languageModerator', 'Language Moderator');
		$role->addChild('languageUpdate');
		$role->addChild('languageDelete');
		$role->addChild('languageManage');

		$auth->addItemChild('admin', 'languageModerator');
	}

	public function safeDown() {
		$this->dropTable('languages');

		$auth = Yii::app()->authManager;

		$auth->removeItemChild('admin', 'languageModerator');

		$auth->removeItemChild('languageModerator', 'languageUpdate');
		$auth->removeItemChild('languageModerator', 'languageDelete');
		$auth->removeItemChild('languageModerator', 'languageManage');

		$auth->removeAuthItem('languageUpdate');
		$auth->removeAuthItem('languageDelete');
		$auth->removeAuthItem('languageManage');

		$auth->removeAuthItem('languageModerator');
	}

}